home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / PowerMacOberon feb96 / Source / Lines.Mod (.txt) < prev    next >
Encoding:
Oberon Text  |  1995-12-08  |  3.1 KB  |  85 lines  |  [TEXT/.Ob4]

  1. Syntax10.Scn.Fnt
  2. FoldElems
  3. Syntax10.Scn.Fnt
  4. (*-----------------------------------------------------------------------
  5. Lines extracts lines containing a certain pattern from a text. This can be used, to get directory
  6. listings with negative selections. For example, in order to get all files except *.Bak files one
  7. does the following:
  8.     System.Directory *
  9.     -- mark the output viewer
  10.     Lines.Separate *.Bak
  11. Lines.Separate (pattern | ^)
  12.     Extracts the lines with the specified pattern from the marked text and displays them in a
  13.     separate viewer. The pattern either follows the command or is described by the selection.
  14.     It contains any character up to the first blank or tab.
  15. -----------------------------------------------------------------------*)
  16. Syntax10i.Scn.Fnt
  17. StampElems
  18. Alloc
  19. 8 Dec 95
  20. Syntax10b.Scn.Fnt
  21. Documentation
  22. MODULE Lines;    (* HM 
  23. IMPORT Texts, Viewers, TextFrames, MenuViewers, Oberon, Strings;
  24. VAR w: Texts.Writer;
  25. PROCEDURE Separate*;    (* [ pattern | "^"] -- removes lines with pattern from marked viewer *)
  26.     VAR t: Texts.Text; r: Texts.Reader; pat, line: ARRAY 128 OF CHAR; ll, pl: INTEGER;
  27.         v: Viewers.Viewer; x, y: INTEGER; eof: BOOLEAN; out: TextFrames.Frame; pos: LONGINT;
  28.     PROCEDURE GetPattern(VAR s: ARRAY OF CHAR);    
  29.         VAR r: Texts.Reader; t: Texts.Text; beg, end, time: LONGINT; ch: CHAR; pl: INTEGER;
  30.     BEGIN
  31.         Texts.OpenReader(r, Oberon.Par.text, Oberon.Par.pos);
  32.         REPEAT Texts.Read(r, ch) UNTIL (ch > " ") OR (ch = 0DX);
  33.         IF ch = "^" THEN
  34.             Oberon.GetSelection(t, beg, end, time);
  35.             IF time >= 0 THEN Texts.OpenReader(r, t, beg); Texts.Read(r, ch) ELSE ch := 0X END
  36.         END;
  37.         pl := 0; WHILE ch > " " DO s[pl] := ch; INC(pl); Texts.Read(r, ch) END;
  38.         s[pl] := 0X
  39.     END GetPattern;
  40.     PROCEDURE GetText(VAR t: Texts.Text);    
  41.     BEGIN
  42.         v := Oberon.MarkedViewer();
  43.         IF (v # NIL) & (v.dsc.next IS TextFrames.Frame) THEN t := v.dsc.next(TextFrames.Frame).text
  44.         ELSE t := NIL
  45.         END
  46.     END GetText;
  47.     PROCEDURE GetLine(VAR s: ARRAY OF CHAR; VAR ll: INTEGER; VAR eof: BOOLEAN);    
  48.         VAR i: INTEGER; ch: CHAR;
  49.     BEGIN i := 0;
  50.         IF ~ eof THEN
  51.             LOOP
  52.                 Texts.Read(r, ch); IF (ch = 0DX) OR (ch = 0X) THEN EXIT END;
  53.                 s[i] := ch; INC(i)
  54.             END;
  55.             eof := ch = 0X
  56.         END;
  57.         s[i] := 0X; ll := i
  58.     END GetLine;
  59.     PROCEDURE WriteString (VAR s: ARRAY OF CHAR);
  60.         VAR i: INTEGER; ch: CHAR;
  61.     BEGIN
  62.         i := 0; ch := s[0];
  63.         WHILE ch # 0X DO Texts.Write(w, ch); INC(i); ch := s[i] END
  64.     END WriteString;
  65. BEGIN
  66.     GetPattern(pat); GetText(t);
  67.     IF (pat # "") & (t # NIL) THEN
  68.         out := TextFrames.NewText(TextFrames.Text(""), 0);
  69.         Texts.OpenReader(r, t, 0); pos := 0; eof := FALSE;
  70.         LOOP
  71.             GetLine(line, ll, eof); IF eof & (ll = 0) THEN EXIT END;
  72.             IF (ll > 0) & Strings.Match(line, pat) THEN
  73.                 WriteString(line); Texts.WriteLn(w);
  74.                 Texts.Delete(t, pos, pos + ll + 1); Texts.OpenReader(r, t, pos)
  75.             ELSE pos := pos + ll +1
  76.             END
  77.         END;
  78.         Oberon.AllocateUserViewer(0, x, y);
  79.         v := MenuViewers.New(TextFrames.NewMenu(pat, "System.Close"), out, TextFrames.menuH, x, y);
  80.         Texts.Append(out.text, w.buf)
  81. END Separate;
  82. BEGIN
  83.     Texts.OpenWriter(w)
  84. END Lines.
  85.